﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using FinGu.Testing.ApplicationLayout;
using FinGu.Testing.SoftwareCheck;
using System.IO;

namespace wfa001
{
    public partial class Form1 : Form
    {
        SoftwareCheck _swCheck = new SoftwareCheck();
        MyApplication _myApp;

        string _resultMessage = "";

        //------------------------------------------------------------------
        public Form1()
        {
            //---------------------------------------------------
            InitializeComponent();

            //---------------------------------------------------
            _myApp = new MyApplication(statusStrip1, cobProductContent, axFramerControl1);

            //---------------------------------------------------
            #region 检查软件版本

            if (_swCheck.CheckSoftWaveVersion(ref _resultMessage) == true)
            {
                this.Text = _resultMessage;
            }
            else
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Enabled = false;
                return;
            }

            #endregion

            #region 检查软件是否有最新版本

            if (_swCheck.Check_SoftWareUpdatListWhetherUpdate(ref _resultMessage))
            {
                MessageBox.Show(_resultMessage);
                return;
            }

            #endregion

            //---------------------------------------------------
            #region 设置FTP服务器登录信息，默认为大车间

            if (!_myApp.InitFtpServerInfo(MyApplication.Shop_Server.BigShop, MyApplication.Shop_Server.UserName,
                MyApplication.Shop_Server.PassWord, ref _resultMessage))
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            #endregion

            //---------------------------------------------------
            #region 将ftp服务器中的config文件加载到窗口的list中。

            if (_myApp.LoadConfigFileToList(ref _resultMessage) == false)
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } 

            #endregion

            //---------------------------------------------------
            #region 创建一个工作区域文件夹，用来装载下载的配置文件模板和config文件。

            if (_myApp.CreateDirection(Directory.GetCurrentDirectory() + "\\WorkArea\\", ref _resultMessage) == false)
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            #endregion

            //---------------------------------------------------
            this.WindowState = FormWindowState.Maximized;
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose(true);
            this.Close();
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            //---------------------------------------------------
            //从指定服务器上下载自动化测试配置模板
            //---------------------------------------------------
            _myApp.DownLoadConfigBoard();

            //---------------------------------------------------
            //从指定FTP上下载指定 产品、工序、版本的测试模板
            //---------------------------------------------------
            _myApp.DownLoadSpecialConfigFile();

            //---------------------------------------------------
            //将产品的测试配置文件内容拷贝到测试配置文件模板中
            //---------------------------------------------------
            _myApp.ConfigFileCopyToConfigBoard();
        }
        private void RepairConfigFile_Click(object sender, EventArgs e)
        {
            string RepairPassWords = Interaction.InputBox("请输入管理员密码！", "提示", "123123", -1, -1);

            if (RepairPassWords == MyApplication.GetManagerPassWord())
            {
                //---------------------------------------------------
                Form2 frm2 = new Form2();
                frm2.Show();
            }
            else if (RepairPassWords != MyApplication.GetManagerPassWord())
            {
                //---------------------------------------------------
                MessageBox.Show("密码输入错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        private void Server_BigShop_Click(object sender, EventArgs e)
        {
            if (Server_BigShop.Text.Contains("*"))
            {
                Server_BigShop.Text = Server_BigShop.Text;
            }
            else
            {
                Server_BigShop.Text = Server_BigShop.Text + "*";
            }

            Server_SamllShop.Text = Server_SamllShop.Text.Replace("*", "");

            #region 初始化FTP服务器信息

            if (!_myApp.InitFtpServerInfo(MyApplication.Shop_Server.BigShop, MyApplication.Shop_Server.UserName,
                MyApplication.Shop_Server.PassWord, ref _resultMessage))
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            #endregion

            #region 将ftp服务器中的config文件加载到窗口的list中。

            if (_myApp.LoadConfigFileToList(ref _resultMessage) == false)
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            #endregion

        }
        private void Server_SamllShop_Click(object sender, EventArgs e)
        {
            if (Server_SamllShop.Text.Contains("*"))
            {
                Server_SamllShop.Text = Server_SamllShop.Text;
            }
            else
            {
                Server_SamllShop.Text = Server_SamllShop.Text + "*";
            }

            Server_BigShop.Text = Server_BigShop.Text.Replace("*", "");

            #region 初始化FTP服务器信息

            if (!_myApp.InitFtpServerInfo(MyApplication.Shop_Server.SmallShop, MyApplication.Shop_Server.UserName,
                MyApplication.Shop_Server.PassWord, ref _resultMessage))
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            #endregion

            #region 将ftp服务器中的config文件加载到窗口的list中。

            if (_myApp.LoadConfigFileToList(ref _resultMessage) == false)
            {
                MessageBox.Show(_resultMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            #endregion
        }
        private void Menu_Update_Click(object sender, EventArgs e)
        {
            string msg;
            if (_swCheck.SetUpSoftWareUpdateListApp(out msg) == false)
            {
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
